package com.ljkj.blockpay.presenter;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.util.JsonBean;
import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.kongzue.baseokhttp.util.Parameter;
import com.ljkj.blockpay.basedata.BasePresenter;
import com.ljkj.blockpay.basedata.BasePresenterImpl;
import com.ljkj.blockpay.model.RechargeModel;
import com.ljkj.blockpay.network.ApiToCConfig;
import com.ljkj.blockpay.presenter.inter.PaymentInter;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yp on 2022/3/17
 */
public class PaymentPresenter extends BasePresenterImpl<PaymentInter.PaymentInterView> implements PaymentInter.PaymentInterPresenter {

    public PaymentPresenter(@NonNull Context context, @NonNull PaymentInter.PaymentInterView view) {
        super(context, view);
    }

    //订单
    @Override
    public void ordercreate(Integer orderType, String preOrderId, String tradePwd) {
        if (preOrderId == null){
            ToastUtils.s(getContext(),"未获取到订单id");
            return;
        }
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("orderType",orderType);
        jsonMap.put("preOrderId",preOrderId);
        jsonMap.put("tradePwd",tradePwd);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_ORDER_CREATE, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    JsonMap jsonMapData =   main.getJsonMap("data");
                    getView().getOrderDetails(jsonMapData);
                }
            }
        });
    }
    //订单预创建
    @Override
    public void orderPreconfirm(Number originAmount, Integer orderType, String originCurrency, String remark) {
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("bizToken","");
        jsonMap.put("orderType",orderType);
        jsonMap.put("networkType","");
        jsonMap.put("originAddress","");
        jsonMap.put("originAmount",originAmount);
        jsonMap.put("originCurrency",originCurrency);
        jsonMap.put("remark",remark);
        jsonMap.put("targetAddress","");
        jsonMap.put("targetCurrency","");
        jsonMap.put("targetUser","");
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_ORDER_PRECONFIRM, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0 ){
                    JsonMap jsonMapData = main.getJsonMap("data");
                    getView().preConfirmSuccess(jsonMapData);
                }
            }
        });
    }
    //网络精度
    @Override
    public void networkAccurary() {
        HttpRequest.GET(getContext(), ApiToCConfig.URL_TRADE_NETWORK_ACCURACY, new Parameter().add("orderType", "5"), new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    JsonList jsonList = main.getList("data");
                    List<RechargeModel> list = new ArrayList<>();
                    for (int i = 0; i < jsonList.size(); i++) {
                        list.add(JsonBean.getBean(jsonList.getJsonMap(i), RechargeModel.class));
                    }
                    getView().showNetAccuracy(list);
                    Log.d("zdl","==onSafeSuccess==="+list.size());
                }else{
                }
            }
        });
    }
    //查询币种余额
    @Override
    public void searchCurrencyBalance(String currencyType, Integer orderType) {
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("currencyType",currencyType);
        jsonMap.put("orderType",orderType);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_SEARCH_CURRENCY, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    JsonMap jsondata= main.getJsonMap("data");
                    getView().SearchCurrencySuccess(jsondata);

                } else {

                }
            }
        });
    }
}
